package reduce.fz.com.reduce.activity;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.HashMap;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.Suggestion;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.service.TeamService;
import reduce.fz.com.reduce.util.InputMethodUtil;
import reduce.fz.com.reduce.util.ServiceBuilder;
import reduce.fz.com.reduce.util.ToastUtil;

public class SuggestionActivity extends BaseToolBarActivity implements View.OnClickListener {
    //主题
    private EditText mSubject;
    //建议
    private EditText mSuggestion;
    //联系方式
    private EditText mContact;
    private TextView mSubmit;
    private TeamService teamService = ServiceBuilder.builderTeamService(null);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_suggestion);
        setToolbarTitle("意见反馈");
        initWidget();
    }

    @Override
    public void initWidget() {
        mSubject = (EditText) findViewById(R.id.et_subject);
        mSuggestion = (EditText) findViewById(R.id.et_suggestion);
        mContact = (EditText) findViewById(R.id.et_contact);
        mSubmit = (TextView) findViewById(R.id.tv_submit);
        mSubmit.setOnClickListener(this);
    }

    public Boolean check() {
        if (TextUtils.isEmpty(mSubject.getText().toString())) {
            ToastUtil.showMessage("写下话题吧~");
            InputMethodUtil.reuestFocusAndShowInput(mSubject);
            return false;

        } else if (TextUtils.isEmpty(mSuggestion.getText().toString())) {
            ToastUtil.showMessage("没有内容喔~");
            InputMethodUtil.reuestFocusAndShowInput(mSuggestion);
            return false;

        } else if (TextUtils.isEmpty(mContact.getText().toString())) {
            ToastUtil.showMessage("留下您的联系方式吧~");
            InputMethodUtil.reuestFocusAndShowInput(mContact);
            return false;

        } else {
            return true;
        }
    }

    public void submit() {
        if (!check()) return;
        Suggestion suggestion = new Suggestion();
        suggestion.subject = mSubject.getText().toString();
        suggestion.suggestion = mSuggestion.getText().toString();
        suggestion.contact = mContact.getText().toString();
        teamService.suggestion(suggestion, new AbResponseListener<HashMap>(new TypeReference<Result<HashMap>>() {
        }) {
            @Override
            public void error(ServiceException e) {

            }

            @Override
            public void responseInfo(Result<HashMap> data) {
                ToastUtil.showMessage("谢谢您的宝贵意见，我们会做的更好!");
                finish();

            }
        });


    }


    @Override
    public void initListener() {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.tv_submit:
                submit();
                break;
            default:
                break;
        }

    }
}
